#include "DialogDataMonitor.h"
#include "ui_DialogDataMonitor.h"

DialogDataMonitor::DialogDataMonitor(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogDataMonitor)
{
    ui->setupUi(this);

    QObject::connect(ui->buttonOk, 
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonOk(bool ))
                     );
    QObject::connect(ui->buttonCancel, 
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonCancel(bool ))
                     );
    QObject::connect(ui->listWidget, 
                     SIGNAL(currentItemChanged(QListWidgetItem* , QListWidgetItem*) ),
                     this,
                     SLOT(slotItemChange(QListWidgetItem* , QListWidgetItem*) )
                     );

    ui->splitter->setStretchFactor(0, 0);
    ui->splitter->setStretchFactor(1, 1);
    // WidgetVariantMatrix* wPtr = new WidgetVariantMatrix;
    // QListWidgetItem* item = new QListWidgetItem();
    // ui->listWidget->addItem(item);
    // ui->listWidget->setItemWidget(item, wPtr);
}

DialogDataMonitor::~DialogDataMonitor()
{
    delete ui;
}

void DialogDataMonitor::addToList(const QString& str)
{
    ui->listWidget->addItem(str);
}

void DialogDataMonitor::tryCustomPlot1()
{
    QVector<double> x(101), y(101);
    for (int i=0; i<101; ++i)
    {
      x[i] = i/50.0 - 1;
      y[i] = x[i]*x[i];
    }
    ui->customPlot->addGraph();

    ui->customPlot->graph(0)->setData(x, y);
    ui->customPlot->graph(0)->setName("Example1");

    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");

    ui->customPlot->xAxis->setRange(-1, 1);
    ui->customPlot->yAxis->setRange(0, 1);
    ui->customPlot->legend->setVisible(true); //

    ui->customPlot->replot();

}

void DialogDataMonitor::tryCustomPlot2()
{
}

void DialogDataMonitor::slotButtonOk(bool value)
{
    
    this->accept();
}

void DialogDataMonitor::slotButtonCancel(bool value)
{
    this->accept();
}

void DialogDataMonitor::slotItemChange(QListWidgetItem* current, QListWidgetItem* previous)
{
    QString text = current->text();
    if(text == "example1")
    {
        this->tryCustomPlot1();
    }
    else if(text == "example2")
    {
        this->tryCustomPlot2();
    }
}

